package com.nightpiter.view.dialogs;

import java.util.Map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * TransparentDialog class.
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class TransparentDialog extends LinearLayout {

	private static final int PADDING_LEFT = 1;
	private static final int PADDING_TOP = 0;
	private static final int PADDING_RIGHT = 1;
	private static final int PADDING_BOTTOM = 4;
	private static final int BORDER_WIDTH = 2;

	RectF drawRect = new RectF();

	/**
	 * paints to draw the rectangle
	 */
	private Paint innerPaint, borderPaint;

	/**
	 * @param context
	 */
	public TransparentDialog(Context context) {
		super(context);
		init();
	}

	/**
	 * @param context
	 * @param attrs
	 * @param inflateParams
	 */
	@SuppressWarnings("unchecked")
	public TransparentDialog(Context context, AttributeSet attrs,
			Map inflateParams) {
		super(context, attrs, inflateParams);
		init();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		super.dispatchTouchEvent(ev);
		return true;
	}

	private void init() {
		innerPaint = new Paint();
		innerPaint.setARGB(225, 75, 75, 75); // gray
		innerPaint.setAntiAlias(true);

		borderPaint = new Paint();
		borderPaint.setARGB(255, 255, 255, 255);
		borderPaint.setAntiAlias(true);
		borderPaint.setStyle(Style.STROKE);
		borderPaint.setStrokeWidth(BORDER_WIDTH);
		borderPaint.setShadowLayer(3, 0, PADDING_BOTTOM - BORDER_WIDTH,
				Color.BLACK);

		this.setPadding(PADDING_LEFT + BORDER_WIDTH,
				PADDING_TOP + BORDER_WIDTH, PADDING_RIGHT + BORDER_WIDTH,
				PADDING_BOTTOM + BORDER_WIDTH);
	}

	public void show() {
		this.setVisibility(VISIBLE);
	}

	public void hide() {
		this.setVisibility(GONE);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {

		drawRect.set(PADDING_LEFT + BORDER_WIDTH, PADDING_TOP + BORDER_WIDTH,
				getWidth() - PADDING_RIGHT - BORDER_WIDTH, getHeight()
						- PADDING_BOTTOM - BORDER_WIDTH);

		canvas.drawRoundRect(drawRect, 3, 3, innerPaint);
		canvas.drawRoundRect(drawRect, 5, 5, borderPaint);

		super.dispatchDraw(canvas);
	}

}
